<template>
<view>
	<!-- 内容区 -->
	<view class="tab-content">
		<view class=" uni-common-mt">
			<uni-segmented-control :current="current" :values="tabBars" style-type="button"
				active-color="#18bc9c" @clickItem="onClickItem" />
		</view>
		
		<!-- 滑块视图 -->
		<view v-show="current === 0">
			<tableHead :poorder="poorder" prestr="PIN" :order_type="4" ref="thead" :showCheckBtn="true"></tableHead>
		</view>
			
		<view v-show="current === 1">
			<scroll-view scroll-y="true" style="height: 80vh;">
				<view class="form_list">
					<showMaterialList :dataArr="porderItem" :orderType="1" :showShelves="true" flag='in'></showMaterialList>
				</view>
			</scroll-view>
			<view class="swiper-button">
				<button  @click="$refs.thead.toSerial()">选择源单</button>
				<button @click="checkOrder">完成</button>
			</view>
		</view>
	</view>
</view>
</template>

<script>
	import tableHead from "@/component/table-head.vue"
	import showMaterialList from "@/component/show-material-list.vue"
	import {
		uniSegmentedControl
	} from '@dcloudio/uni-ui'
	
	export default {
		components: {
			uniSegmentedControl,showMaterialList,tableHead
		},
		data() {
			return {
				current: 1, /* 选中标签栏的序列,默认显示第一个 */
				tabBars:['表头','表体'],
				
				poorder: {},//源单对象
				porderItem:[],//采购单表体
				
				user:uni.getStorageSync('user')
			}
		},
		computed: {},
		onShow() {
			//监听选中源单
			uni.$off("getPlan")
			uni.$once("getPlan", (value) => {
				this.poorder = value
				this.$refs.thead.remark = value.remark //源单的备注，赋值pda备注
				
				uni.showLoading();
				//获得表体记录
				this.$Db.selectTableData('procure', 'pcode', value.pcode).then(res => {
					this.porderItem = []//先清空之前
					res.forEach((arr) => {
						if(arr.no_quantity>0){
							//有未操作数量，才加入
							arr.real_quantity = arr.no_quantity
							arr.real_store_quantity = Number(arr.no_quantity/arr.fCoefficient)//常用数量
							
							//建议货架
							arr.tj_shelves_id = arr.shelves_id
							arr.shelves_id = 0
							arr.tj_shelves_name = arr.warehouse_name +'-'+arr.shelves_name
							arr.shelves_name = ''
							
							//最新货架
							arr.lastest_shelves_id = arr.lastest_shelves_id
							arr.lastest_shelves_name = arr.lastest_warehouse_name +'-'+arr.lastest_shelves_name
							
							arr.cBatch = this.$Func.getDateTime('today','Ymd')//批号默认当天日期
							//有启用批号
							// if(arr.mbatch){
							// 	arr.cBatch = this.$Func.getDateTime('today','Ymd')//批号默认当天日期
							// }else{
							// 	arr.cBatch = ''
							// }
							
							this.porderItem.push(arr)
						}
					});
					uni.hideLoading();
				})
			})
		},
		onLoad(options) {
			this.$Func.setBarTitle('新增产品入库')
			setTimeout(()=>{
				this.$refs.thead.init() //初始化
			},500)
		},
		methods: {
			//切换标签内容
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			//核实收货单数据
			async checkOrder() {
				let _this = this;
				let check = []
				let check_total = 0 //总数不能为0
				let billentry = []
				let overflag = false
				let check_invclass_arr = []
				
				if(this.porderItem.length == 0){
					this.$Api.msg('请先选择源单','error')
					return
				}
				
				for(let i in this.porderItem){
					let citem = this.porderItem[i]
					//没输入数量的那些就是undefined
					if(isNaN(citem.real_quantity) || citem.real_quantity == 0){
						continue
					}
					
					if(isNaN(citem.shelves_id)||citem.shelves_id==0){
						this.$Api.msg(citem.inv_name+",请选择库区")
						return 
					}
					 
					//物料属性有设置批号
					if(citem.mbatch&&this.$Func.trim(citem.cBatch)==''){
						this.$Api.msg(citem.inv_name+"有启用批号属性,批号不能为空")
						return 
					}
					
					check_total += parseFloat(citem.real_quantity)//累加总数
					
					//实收数量超过应收提示   都能超数量
					// if(parseFloat(citem.real_quantity)>parseFloat(citem.no_quantity)){
					// 	//overflag = true
					// 	this.$Api.msg(citem.inv_name+',实收数量超应收数量')
					// 	return 
					// }
					
					//判断货架物料分类策略数组
					check_invclass_arr.push({
						'name':citem.inv_name,
						'number':citem.inv_number,
						'shelves_id':citem.shelves_id,
						'classification_id':citem.classification_id
					})
					
					//单据表体添加
					billentry.push({
						'FBillNo':this.$refs.thead.FBillNo,
						'auto_id':citem.auto_id,//u8表体内码自增id
						'row_id':citem.row_id,
						'local_row_id':i,
						'inv_number':citem.inv_number,
						'inv_name':citem.inv_name,
						'inv_specification':citem.inv_specification,
						'inv_attributes':citem.inv_attributes,
						'inv_unit':citem.unit_name,
						'inv_unit_code':citem.unit_code,
						'store_unit':citem.store_unit,
						'store_unit_code':citem.store_unit_code,
						'fqty':citem.real_quantity,
						'fauxqty':citem.real_store_quantity,
						'cBatch':citem.cBatch,
						'shelves_id':citem.shelves_id,
						'shelves_type':citem.shelves_type,
						'shelves_name':citem.shelves_name,
						'b_remark':''
					})
				}
				
				if(!check_total||!billentry.length||isNaN(check_total)){
					this.$Api.msg("入库数量全为0",'error')
					return 
				}
				check_total = this.$Func.decimalPattern(check_total)//小数点后4位
				
				let bill = {//准备表头数据
					//'auto_id':this.poorder.auto_id,
					'source_code':this.poorder.pcode,//采购源单编号
					'order_type':4,//产品入库单
					'FBillNo':this.$refs.thead.FBillNo,//PDA单据编号
					'FDate':this.$refs.thead.FDateVal,
					'a_remark':this.$refs.thead.remark,
					'stock_id':this.$refs.thead.stock_id,
					'status':1,
					'syn_status':0,
					'total_qty':check_total,
					'factory_code':this.poorder.factory_code,
					'factory_name':this.poorder.factory_name,
					'userid':this.user.userid,
					'create_date':this.$Func.getDateTime('today','Y-m-d')
				}
				
				this.addOrder(bill,billentry,check_invclass_arr)
				/*if(overflag){
					uni.showModal({
						title: '温馨提醒',
						content: '存在实收数量超应收数量，是否继续？',
						success: function (res) {
							if (res.confirm) {
								_this.addOrder(bill,billentry,check_invclass_arr)
							}
						}
					}); 
				}else{
					this.addOrder(bill,billentry,check_invclass_arr)
				}*/
			},
			//添加本地单据
			async addOrder(bill,billentry,check_invclass_arr){
				//判断货架物料分类策略
				// let msg = ''
				// await this.$Db.checkShelvesInvclass(check_invclass_arr).catch(err=>{
				// 	this.$Db.addlog('添加产品入库',err)
				// 	msg = err
				// })
				// if(msg){
				// 	this.$Api.msg(msg)
				// 	return
				// }
				 
				uni.showLoading({mask:true})
				await this.$Db.addOrder(4,bill,billentry).then(res=>{
					this.$Api.msg('添加成功','success')
					this.reset()
					uni.hideLoading();
				}).catch(err=>{
					this.$Db.addlog('添加产品入库',err)
					this.$Api.msg('添加失败','error')
					uni.hideLoading();
				})
			},
			reset(){
				this.poorder = {}
				this.porderItem = []
				this.$set(this.poorder, 'pcode', ''); // 清空对象属性
				this.$set(this.poorder, 'factory_name', ''); // 清空对象属性
				this.$refs.thead.reset() //初始化
			}
		}
	}
</script>
<style lang="scss">
</style>